﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;

namespace RefreshExcel
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
#if DEBUG
            //int processId = System.Diagnostics.Process.GetCurrentProcess().Id;
            //string message = string.Format("Please attach the debugger to process [{0}].", processId);
            //System.Windows.Forms.MessageBox.Show(message, "Debug");
#endif
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            SystemPaths = SystemPaths.Concat(MyPath);
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);
            SystemPaths = SystemPaths.Except(MyPath);
        }

        public override void Rollback(System.Collections.IDictionary savedState)
        {
            base.Rollback(savedState);
            SystemPaths = SystemPaths.Except(MyPath);
        }

        private static IEnumerable<string> SystemPaths
        {
            get
            {
                return Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine).Split(';');
            }
            set
            {
                var newPath = string.Join(";", value.Distinct().Where(s => !string.IsNullOrEmpty(s)).ToArray());
                System.Environment.SetEnvironmentVariable("Path", newPath, EnvironmentVariableTarget.Machine);
            }
        }

        private static readonly string[] MyPath = new[] { System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) };
 
    }
}
